package com.hzqy.web.ac.epg;


import com.alibaba.fastjson.JSONArray;
import com.hzqy.commons.utils.ExcelUtil;
import com.hzqy.commons.utils.ExcelUtil.MyEntry;
import com.hzqy.commons.utils.FileType;
import com.hzqy.service.epg.AcSilverService;
import com.hzqy.web.vo.epg.AcSilverVo;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/acSilver")
public class AcSilverAction {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	private static final String[] allowedFileType = new String[]{"png", "jpg", "gif","zip","jar","tar","docx","doc","xlsx","xls","flv","mp4","mp3","mpg","avi","mov","wmv"};
    @Resource
	private AcSilverService serviceAction;
    @RequestMapping("/acSilverSelectAll")
	@ResponseBody
	public Object acSignSelect(AcSilverVo as, int page, int rows){
		Map<String,Object> SignListMap = new HashMap<String, Object>();
		SignListMap= serviceAction.selectAc(as,page, rows);
		return SignListMap;
	}
	@RequestMapping("/acSilverExcel")
	@ResponseBody
	public void acSilverExcel(AcSilverVo infoVo, HttpSession session, int page, int rows, HttpServletResponse response) throws IOException {
		Map<String,Object> ListMap = new HashMap<String, Object>();
    	ListMap = serviceAction.selectAc(infoVo,1,100000);
		List<AcSilverVo> data=(List<AcSilverVo>)ListMap.get("rows");
		List<MyEntry<String, String>> excelHeader=new ArrayList<MyEntry<String, String>>();
		excelHeader.add(new MyEntry<String,String>("f_as_id","ID"));
		excelHeader.add(new MyEntry<String,String>("f_as_cardnumber","券"));
		excelHeader.add(new MyEntry<String,String>("f_as_cardkey","用户ITV"));
		excelHeader.add(new MyEntry<String,String>("f_as_effectivetime","结束时间"));
		excelHeader.add(new MyEntry<String,String>("f_as_price","上线日期"));
		excelHeader.add(new MyEntry<String,String>("f_as_status","有效状态"));
		excelHeader.add(new MyEntry<String,String>("f_as_cdate","存入时间"));
		excelHeader.add(new MyEntry<String,String>("f_as_udate","使用时间"));
		boolean flag= ExcelUtil.generateExcel(data, excelHeader, response);
	}
	/*@RequestMapping("/importSilver")
	@ResponseBody
	public Object insertBatchSilver(@RequestParam(value = "uploadify") MultipartFile file ,HttpServletResponse response) throws Exception {
		response.setContentType("text/html;charset=UTF-8");
		if (FileType.checkFileType(file.getInputStream(), allowedFileType)) {
			int successCount = 0;
			StringBuffer str = new StringBuffer();
			try {
				LOG.debug("批量导入Silver开始");
				List<Map<String, Object>> list = ExcelUtil.parseExcel(file.getInputStream(), ExcelUtil.ALLOWED_EMPTY_MODE, new String[]{});
				LOG.debug("解析excel完成, 开始循环入库" + list.size());
				for (int i = 0; i < list.size() - 1; i++) {
					AcSilverVo Silver = new AcSilverVo();
					Map<String, Object> map = list.get(i);
					Silver.setF_as_cardnumber(map.get("券").toString());
					Silver.setF_as_price(map.get("上线日期").toString());
					Silver.setF_as_effectivetime(map.get("结束时间").toString());
					boolean success = false;
					Map<String, Object> resultMap = serviceAction.insertSilverService(Silver);
					success = (Boolean) resultMap.get("success");
					successCount += (success ? 1 : 0);
				}
				LOG.debug("批量完成" + successCount);
				str.append("一共").append(list.size() - 1).append("条数据").append("批量导入完成,：").append(successCount).append("条数据成功").append(list.size() - 1 - successCount).append("条失败");
			} catch (Exception e) {
				LOG.error("", e);
				str.append("批量导入失败: ");
			}
			return str.toString();
		} else {
			return "";
		}
	}*/

}
